import { showToast } from "utils/index";
import {
  GetSongDetail,
  GetSongLyrics,
  GetSongLyricsNew,
  GetSongUrl,
} from "apis/index";
const app = getApp();
Page({
  data: {
    id: "", //音乐id
    tabs: ["推荐", "歌曲", "歌词"],
    currTabInd: 1,
    isLove: false,
    isPlaying: false,
    showSharePopup: false,
    playOrder: "sxbf", //播放顺序：sjbf(随机播放), dqxh(单曲循环), sxbf(顺序播放)
    lyrics: [], //歌词句子
    progress: 0,
    currTime: 0, //单位：毫秒
    songInfo: {
      img: "", //海报图片
      src: "", //音频地址
      name: "", //歌曲名称
      singers: "", //歌手名称（可能有多个）
      duration: 0, //单位：毫秒
    },
    currPlayInd: 2,
  },

  onLoad(options) {
    const { id } = options;
    this.audioCtx = wx.createInnerAudioContext("audio");
    this.setData({
      id,
    });
    this.init();
  },
  onShow() {
    this.setData({
      isPlaying: app.globalData.isPlaying,
    });
  },
  onShareAppMessage() {},
  //初始化页面数据
  init() {
    const { id } = this.data;
    GetSongUrl({ id }).then((res) => {
      console.log(res, "音乐地址--------");
      this.setData({
        "songInfo.src": res[0]?.url,
      });
    });
    GetSongDetail({ ids: id }).then((res) => {
      const { name, dt, ar, al } = res.songs[0];
      console.log(res, dt, "歌曲详情res-------------------");
      this.setData({
        "songInfo.img": al.picUrl,
        "songInfo.name": name,
        "songInfo.singers": ar.map((it) => it.name).join(" - "),
        "songInfo.duration": dt,
      });
      this.updateProgress();
    });
    GetSongLyrics({ id }).then((res) => {
      const { lyric } = res.lrc;
      const list = lyric.split("\n").map((item) => {
        const [time, text] = item.split("]");
        return {
          time: time.slice(1),
          text: text?.trim(),
        };
      });
      console.log(res, "lrc----------");
      this.setData({
        lyrics: list,
      });
    });
    // GetSongLyricsNew({id}).then(res=>{
    //   const { lyric } = res.lrc;
    //   console.log(res,lyric,'res----柱子歌词')
    // })
  },
  //切换海报封面
  changeTab(e) {
    const { ind } = e.currentTarget.dataset;
    this.setData({
      currTabInd: ind,
    });
  },
  //播放音乐
  playMusic(time) {
    if (time) {
      this.audioCtx.seek(14);
    } else {
      this.audioCtx.play();
    }
  },
  //暂停音乐
  pauseMusic() {
    this.audioCtx.pause();
  },
  //滑块进度改变后
  onSliderChange(e) {
    const { value } = e.detail;
    const { duration } = this.data.songInfo;
    // this.playMusic(duration * (value / 100));
  },
  //滑块进度改变中
  onSliderChanging(e) {
    const { value } = e.detail;
    const { duration } = this.data.songInfo;
    this.setData({
      currTime: duration * (value / 100),
    });
  },
  //更新播放进度
  updateProgress() {
    const { songInfo, currTime } = this.data;
    const { duration } = songInfo;
    this.setData({
      progress: Math.floor((currTime / duration) * 100),
    });
  },
  onPrev() {},
  onNext() {},
  //切换播放状态(暂停、播放)
  togglePlayStatus() {
    const isPlaying = !this.data.isPlaying;
    isPlaying ? this.playMusic() : this.pauseMusic();
    this.setData({
      isPlaying,
    });
    app.globalData.isPlaying = isPlaying;
  },
  //播放过程中播放时间变化
  onPlayTimeUpdate(e) {
    const { currentTime, duration } = e.detail;
    console.log(currentTime, "currentTime-播放中----------------");
  },
  //切换喜欢状态（是否喜欢）
  toggleLove() {
    const { isLove } = this.data;
    showToast(isLove ? "已从喜欢列表移除" : "已添加进喜欢列表");
    this.setData({
      isLove: !isLove,
    });
  },
  //切换播放顺序(顺序播放、随机播放、单曲循环)
  togglePlayOrder() {
    const { playOrder } = this.data;
    const tipsMap = {
      sxbf: "已切换为随顺序播放",
      sjbf: "已切换为随机播放",
      dqxh: "已切换为单曲循环",
    };
    const allPlayOrders = Object.keys(tipsMap);
    const ind = allPlayOrders.indexOf(playOrder);
    let nextInd = ind + 1;
    if (nextInd > allPlayOrders.length - 1) {
      nextInd = 0;
    }
    showToast(tipsMap[allPlayOrders[nextInd]]);
    this.setData({
      playOrder: allPlayOrders[nextInd],
    });
  },
  //打开分享弹窗
  openSharePopup() {
    this.setData({
      showSharePopup: true,
    });
  },
});
